/*
 * @Author: ZhaoZhiqi
 * @Date: 2022-08-09 09:47:21
 * @LastEditors: ZhaoZhiqi
 * @LastEditTime: 2022-12-13 15:16:23
 * @Description: 用户store
 * @FilePath: \tuanfeng-admin-front\src\store\role.js
 */
import { defineStore } from "pinia";
import { loginAPI, LogoutAPI, getMenusAPI } from "../api/log"
import {
    Tickets,
    Message,
    ChatDotRound,
    OfficeBuilding,
    Briefcase,
    Document,
    TakeawayBox,
    Phone,
    User,
    Avatar,
    CopyDocument,
    DocumentCopy,
    MessageBox,
    Reading,
    Notebook,
    Histogram,
    FolderOpened,
    CollectionTag,
    UserFilled,
    Postcard,
    Camera,
} from "@element-plus/icons-vue";

const IconMap = {
    Tickets,
    Message,
    ChatDotRound,
    OfficeBuilding,
    Briefcase,
    Document,
    TakeawayBox,
    Phone,
    User,
    Avatar,
    CopyDocument,
    DocumentCopy,
    MessageBox,
    Reading,
    Notebook,
    Histogram,
    FolderOpened,
    CollectionTag,
    UserFilled,
    Postcard,
    Camera,
};

export const useRoleStore = defineStore({
    id: "role",
    state: () => ({
        nick_name: "",
        permissions: {},
        routes: null
    }),
    actions: {
        // 登录
        Login(name, password) {
            return new Promise((resolve, reject) => {
                loginAPI({ name, password }).then((res) => {
                    this.nick_name = name;
                    let token = res.data.Authorization;
                    localStorage.setItem("tf_admin_name", name);
                    localStorage.setItem("tf_admin_token", token);
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            });
        },
        getMenus() {
            return new Promise((resolve, reject) => {
                getMenusAPI().then(res => {
                    let routes = []

                    res.data && res.data.forEach((item) => {
                        // 处理权限
                        this.permissions[item.path] = {}
                        item.btns && item.btns.forEach(btn => {
                            this.permissions[item.path][btn.name] = true;
                        })

                        // 处理菜单
                        let route = {
                            path: item.path,
                            name: item.component,
                            meta: {
                                title: item.name,
                                icon: IconMap[item.icon],
                                type: 'page',
                            },
                            component: () => import(`../pages/${item.component}/index.vue`),
                        };
                        routes.push(route);
                    });
                    this.routes = routes;
                    
                    resolve(routes);
                }).catch(err => {
                    reject(err);
                })
            })
        },
        // 登出
        Logout() {
            LogoutAPI().then(res => {
                this.routes = null;
                localStorage.removeItem("tf_admin_name");
                localStorage.removeItem("tf_admin_token");
                this.nick_name = "";
                location.href = '/#/log'
            })
        }
    }
});